﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OneMake.Model;
using OneMake.DataAccess;

namespace OneMake.Winforms
{
    public partial class uc_report_type_sale : UserControl, IBaseReportCtrl
    {
        private DataHelper dataHelper = new DataHelper();
        private ReportHelper rh = new ReportHelper();

        public uc_report_type_sale()
        {
            InitializeComponent();
        }

        private void uc_report_gplxs_Load(object sender, EventArgs e)
        {
            dtpBegin.Value = DateTime.Now.AddDays(-7);
            dtpEnd.Value = DateTime.Now;
            LoadShops();
            cbType.SelectedIndex = 0;
        }

        private void LoadShops()
        {
            cbShops.DataSource = dataHelper.GetShops();
            cbShops.DisplayMember = "name";
            cbShops.ValueMember = "shopid";
            if (cbShops.Items.Count > 0)
                cbShops.SelectedIndex = 0;
        }

        public string ReportName
        {
            get { return "report_type_sale"; }
        }

        public DockStyle Dock2
        {
            get { return this.Dock; }
            set { this.Dock = value; }
        }

        public Control Parent2
        {
            get { return this.Parent; }
            set { this.Parent = value; }
        }
        public void LoadData() { }
        public void LoadData(string type)
        {
            int selShopId = 0;
			lbProgress.Visible = true;
			panel1.Refresh();
			
			this.dgvReport.DataSource = null;
			this.dgvReport.Refresh();
			
			if (cbShops.SelectedValue != null)
                selShopId = System.Convert.ToInt32(cbShops.SelectedValue.ToString());
            
            string type1 = cbType.Text;
            
            DateTime selBeginTime = dtpBegin.Value;
            DateTime selEndTime = dtpEnd.Value;

            List<report_type_sale> reports = rh.get_report_gplxs_sale_num(selShopId, selBeginTime, selEndTime);
            foreach (report_type_sale report in reports)
            {
                int idx = dgvReport.Rows.Add();
                DataGridViewRow row = dgvReport.Rows[idx];
                row.Cells[type].Value = report.type;

                if (type == "销售数量")
                {
                    foreach (KeyValuePair<DateTime, int> val in report.sale_nums)
                        row.Cells[val.Key.ToShortDateString()].Value = val.Value;
                }

                if (type == "销售金额")
                {
                    foreach (KeyValuePair<DateTime, double> val in report.sale_amounts)
                        row.Cells[val.Key.ToShortDateString()].Value = val.Value;
                }
            }

            lbProgress.Visible = false;
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            int selShopId = 0;
			lbProgress.Visible = true;
			panel1.Refresh();
			
			this.dgvReport.DataSource = null;
			this.dgvReport.Refresh();
			
			if (cbShops.SelectedValue != null)
                selShopId = System.Convert.ToInt32(cbShops.SelectedValue.ToString());
            
            string reportType = cbType.Text;
            
            DateTime selBeginTime = dtpBegin.Value;
            DateTime selEndTime = dtpEnd.Value;

            initColumn(reportType, selBeginTime, selEndTime);

            LoadData(reportType);
        }

        private void initColumn(string reportType,DateTime beginDate, DateTime endDate)
        {
            List<string> titles = new List<string>();
            titles.Add(reportType);

            DateTime tempDate = beginDate;
            do
            {
                titles.Add(tempDate.ToShortDateString());
                tempDate = tempDate.AddDays(1);
            }
            while (tempDate <= endDate);

            dgvReport.Columns.Clear();
            foreach (string title in titles)
            {
                DataGridViewColumn col = new DataGridViewTextBoxColumn();
                col.Name = title;
                col.HeaderText = title;
                col.DataPropertyName = title;
                dgvReport.Columns.Add(col);
            }
        }
    }
}
